package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.result.Result;
import com.xr.service.MemberService;
import com.xr.service.SetMealService;
import com.xr.utils.DateUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private MemberService memberService;

    @Reference
    private SetMealService setMealService;

    /**
     * 会员折线图
     *
     * @return
     */
    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        Map<String, Object> map = new HashMap<>();

        List<String> year4Month = DateUtils.getLastYear4Month("YYYY.MM");

        List<Integer> memberReport = memberService.getMemberReport(year4Month);

        map.put("months", year4Month);
        map.put("memberCount", memberReport);
        return new Result(true,
                MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest req, HttpServletResponse
            res) throws Exception {
        // 获取运营统计数据
        Map<String, Object> reportData = setMealService.getBusinessReportData();
        // 获取模板的路径, getRealPath("/") 相当于到webapp目录下
        String template = req.getSession().getServletContext().getRealPath("/template/report_template.xlsx");
        // 创建工作簿(模板路径)
        try (// 写在try()里的对象，必须实现closable接口，try()cathc()中的finally
             OutputStream os = res.getOutputStream();
             XSSFWorkbook wk = new XSSFWorkbook(template);) {
            // 获取工作表
            XSSFSheet sht = wk.getSheetAt(0);
            // 日期 坐标 2,5
            sht.getRow(2).getCell(5).setCellValue(reportData.get("reportDate").toString());
            //======================== 会员 ===========================
            // 新增会员数 4,5
            sht.getRow(4).getCell(5).setCellValue(reportData.get("todayNewMember").toString());
            // 总会员数 4,7
            sht.getRow(4).getCell(7).setCellValue(reportData.get("totalMember").toString());
            // 本周新增会员数5,5
            sht.getRow(5).getCell(5).setCellValue(reportData.get("thisWeekNewMember").toString());
            // 本月新增会员数 5,7
            sht.getRow(5).getCell(7).setCellValue(reportData.get("thisMonthNewMember").toString());
            //=================== 预约 ============================
            sht.getRow(7).getCell(5).setCellValue(reportData.get("todayOrderNumber").toString());
            sht.getRow(7).getCell(7).setCellValue(reportData.get("todayVisitsNumber").toString());
            sht.getRow(8).getCell(5).setCellValue(reportData.get("thisWeekOrderNumber").toString());
            sht.getRow(8).getCell(7).setCellValue(reportData.get("thisWeekVisitsNumber").toString());
            sht.getRow(9).getCell(5).setCellValue(reportData.get("thisMonthOrderNumber").toString());
            sht.getRow(9).getCell(7).setCellValue(reportData.get("thisMonthVisitsNumber").toString());
            // 热门套餐
            List<Map<String, Object>> hotSetmeal = (List<Map<String, Object>>) reportData.get("hotSetmeal");
            int row = 12;
            for (Map<String, Object> setmealMap : hotSetmeal) {
                sht.getRow(row).getCell(4).setCellValue((String) setmealMap.get("name"));
                sht.getRow(row).getCell(5).setCellValue((Long) setmealMap.get("setmeal_count"
                ));

                String str = (String) setmealMap.get("proportion");
                System.out.println(str.substring(0,str.length() -1)+"left join");
                str=str.substring(0,str.length() -1);

                BigDecimal proportion = new BigDecimal(str);


                sht.getRow(row).getCell(6).setCellValue(proportion.doubleValue()+"%");
                sht.getRow(row).getCell(7).setCellValue((String) setmealMap.get("remark"));
                row++;
            }
            String filename = "运营统计数据报表.xlsx";
            // 解决下载的文件名 中文乱码
            filename = new String(filename.getBytes(), "ISO-8859-1");
            // 设置头信息，告诉浏览器，是带附件的，文件下载
            res.setHeader("Content-Disposition", "attachement;filename=" +filename);
            // 设置内容体的文件格式是excel
            res.setContentType("application/vnd.ms-excel");
            wk.write(os);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}


